package com.forum.article.controller;


import com.forum.article.entity.ForumArticleTag;
import com.forum.article.mapper.ForumArticleTagMapper;
import com.forum.article.service.ForumArticleTagService;
import com.forum.article.vo.Result;
import com.forum.common.constant.ErrorCodeEnum;
import com.forum.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/article/forumArticleTag")
public class ForumArticleTagController {
    @Autowired
    ForumArticleTagService forumArticleTagService;
    @Autowired
    ForumArticleTagMapper forumArticleTagMapper;

    /**
     * 新增标签
     * @param forumArticleTag
     * @return
     */
    @PostMapping("/addTag")
    public R addTag(@RequestBody ForumArticleTag forumArticleTag) {
        if ( forumArticleTag != null ) {
            forumArticleTagMapper.insert(forumArticleTag);
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.VALID_EXCEPTION.getCode() , ErrorCodeEnum.VALID_EXCEPTION.getMessage());
        }
    }

    /**
     * 修改标签
     * @return
     */
    @PostMapping("/updateTag")
    public R updateTag(@RequestBody ForumArticleTag forumArticleTag) {
        if ( forumArticleTag != null ) {
            forumArticleTagMapper.updateById(forumArticleTag);
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.VALID_EXCEPTION.getCode() , ErrorCodeEnum.VALID_EXCEPTION.getMessage());
        }
    }

    /**
     * 删除标签
     * @return
     */
    @PostMapping("/deleteTag")
    public R deleteTag(@RequestBody Long id) {
        if ( id != null ) {
            forumArticleTagMapper.deleteById(id);
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.VALID_EXCEPTION.getCode() , ErrorCodeEnum.VALID_EXCEPTION.getMessage());
        }
    }


    @GetMapping("hot")
    public R hot(){
//        最热6个标签
        int limit = 6;
        return forumArticleTagService.hots(limit);
    }



    @GetMapping("/allTags")
    public Result findAll(){
        return forumArticleTagService.findAll();
    }

    /**
     * 查询所有标签
     * @return
     */
    @GetMapping("detail")
    public Result findAllDetail(){
        return forumArticleTagService.findAllDetail();
    }

    @GetMapping("detail/{id}")
    public Result findDetailById(@PathVariable("id") Long id){
        return forumArticleTagService.findDetailById(id);
    }
}

